﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using nvn.Core.MVP;
using Hutech.CRM.Library.Views;
using Hutech.CRM.Library.Models.Entities;
using Hutech.CRM.Library.Utilities;
using nvn.Library.Patterns;
using System.Drawing;

namespace Hutech.CRM.Web.Controls.ManageLevelCustomers
{
    public partial class ListCriterionForGroupCustomer : System.Web.UI.UserControl,IListCriterionForGroupCustomerView
    {
        #region MEMBER VARIABLES
        protected string EditPropertiesImage = "~/Data/SiteImages/" + WebConfigSettings.EditPropertiesImage;
        protected string DeleteLinkImage = "~/Data/SiteImages/" + WebConfigSettings.DeleteLinkImage;
        List<TowTypeParameters<Guid, string>> _GroupLevelCustomers;
        #endregion




        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.InitView();

            this.ImageButtonAddNew.Click += new ImageClickEventHandler(ImageButtonAddNew_Click);
            this.DataListCriterionCustomer.ItemCommand += new DataListCommandEventHandler(DataListCriterionCustomer_ItemCommand);
            this.DataListCriterionCustomer.ItemDataBound += new DataListItemEventHandler(DataListCriterionCustomer_ItemDataBound);
            this.AddNewCriterionCustomer1.Reload_EventAgrs += new EventHandler<EventArgs>(AddNewCriterionCustomer1_Reload_EventAgrs);
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.OnFristLoadEventHandler();
            }
        }

        #region All event

        void AddNewCriterionCustomer1_Reload_EventAgrs(object sender, EventArgs e)
        {
            OnReload_EventArgs();
        }

        void DataListCriterionCustomer_ItemCommand(object source, DataListCommandEventArgs e)
        {
            CriterionCustomer item=new CriterionCustomer();

            //Search value control
            if (e.CommandName.Equals(ConstantVariable.COMMAND_APPLY))
            {
                item.CriterionName = ((TextBox)e.Item.FindControl("TextBoxCriterionName")).Text;
                item.GroupLevelCustomerGuid = new Guid(((DropDownList)e.Item.FindControl("DropDownListGroupLevelCustomer")).SelectedValue.ToString());
                item.GroupLevelCustomerName = ((DropDownList)e.Item.FindControl("DropDownListGroupLevelCustomer")).SelectedItem.ToString();
                item.IsActive = ((CheckBox)e.Item.FindControl("CheckBoxIsActive")).Checked;
            }
            if (e.CommandName.Equals(ConstantVariable.COMMAND_ENABLE))
            {
                item.IsActive = ((CheckBox)e.Item.FindControl("CheckBoxIsActive")).Checked;
            }
            
            this.OnListCriterionCustomer_ItemCommand(
                e.CommandName.ToString(), 
                DataListCriterionCustomer.DataKeys[e.Item.ItemIndex].ToString(),
                e.Item.ItemIndex,
                item);
        }
        void DataListCriterionCustomer_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.EditItem)
            {
                DropDownList DropDownListGroupLevelCustomer = (DropDownList)e.Item.FindControl("DropDownListGroupLevelCustomer");
                this.OnListCriterionCustomer_ItemDataBound("LoadGroupLevel");

                DropDownListGroupLevelCustomer.DataSource = GroupLevelCustomers;
                DropDownListGroupLevelCustomer.DataValueField = "T";
                DropDownListGroupLevelCustomer.DataTextField = "V";
                DropDownListGroupLevelCustomer.DataBind();

                //dung co ngu ngoc nhu cho nay nua :D
                DropDownListGroupLevelCustomer.SelectedValue = ((HiddenField)e.Item.FindControl("HiddenFieldKeyGroupLevel")).Value.ToString();
            }
            else
            {
                bool IsActive = bool.Parse(((HiddenField)e.Item.FindControl("HiddenFieldIsActive")).Value.ToString());
                //Set color
                Panel PanelLine = (Panel)e.Item.FindControl("PanelLine");
                if (IsActive)
                    PanelLine.ForeColor = Color.Blue;
                else
                    PanelLine.ForeColor = Color.Black;
                //if (e.Row.RowIndex >= 0)
                //{
                //    e.Row.Attributes.Add("onMouseOver", "this.style.backgroundColor='#bde2fd'");
                //    if (e.Row.RowIndex % 2 == 0)
                //        e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=''");
                //    else
                //        e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=''");
                //}tr.Attributes.Add("style", "cursor:hand;");
            }
        }
        void ImageButtonAddNew_Click(object sender, ImageClickEventArgs e)
        {
            this.AddNewCriterionCustomer1.ShowPopup = true;
        }
        #endregion
        
        #region Implement from View class
        #region Property
        public List<CriterionCustomer> CriterionCustomers
        {
            set { DataListCriterionCustomer.DataSource = value; DataListCriterionCustomer.DataBind(); }
        }
        public List<TowTypeParameters<Guid, string>> GroupLevelCustomers
        {
            get
            {
                return _GroupLevelCustomers;
            }
            set
            {
                _GroupLevelCustomers = value;
            }
        }
        public bool ActiveItem
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public int EditItemIndex
        {
            set { DataListCriterionCustomer.EditItemIndex = value; }
        }
        public string ErrorMessage
        {
            set
            {
                if (value != null)
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Show", "<script type='text/javascript'>alert('" + value + "');</script>", false);
            }
        }
        #endregion

        #region Event
        public event EventHandler<EventArgs> FristLoadEventHandler;
        private void OnFristLoadEventHandler()
        {
            if (FristLoadEventHandler != null)
                FristLoadEventHandler(this, new EventArgs());
        }

        //public event EventHandler<EventArgs> AddNew_EvenAgrs;
        //private void OnAddNew_EvenAgrs()
        //{
        //    if (AddNew_EvenAgrs != null)
        //        AddNew_EvenAgrs(this, new EventArgs());
        //}

        public event EventHandler<ItemCommandArgs<CriterionCustomer>> ListCriterionCustomer_ItemCommand;
        private void OnListCriterionCustomer_ItemCommand(string parameterName, string parameterValue, int itemIndex,CriterionCustomer myType)
        {
            if (ListCriterionCustomer_ItemCommand != null)
                ListCriterionCustomer_ItemCommand(this, new ItemCommandArgs<CriterionCustomer>(parameterName, parameterValue, itemIndex,myType));
        }

        public event EventHandler<ItemDataBoundArgs> ListCriterionCustomer_ItemDataBound;
        private void OnListCriterionCustomer_ItemDataBound(string parameterName)
        {
            if (ListCriterionCustomer_ItemDataBound != null)
                ListCriterionCustomer_ItemDataBound(this, new ItemDataBoundArgs(parameterName));
        }
        
        public event EventHandler<EventArgs> Reload_EventArgs;
        private void OnReload_EventArgs()
        {
            if (Reload_EventArgs != null)
                Reload_EventArgs(this, new EventArgs());
        }
        #endregion
        #endregion

    }
}